<template>
  <div class="module-head-box">
    <div class="head-left">
      <je-form
        :model="sourceForm"
        name="basic"
        style="width: 500px"
        :label-col="{ span: 6 }"
        :wrapper-col="{ span: 18 }"
        autocomplete="off"
        :colon="false"
      >
        <je-form-item
          label="名称"
          name="JE_CORE_DATASOURCE_ID"
          validate-type="tooltip"
          v-bind="validateInfos.DATASOURCE_TEXT"
        >
          <je-input v-model:value="sourceForm.DATASOURCE_TEXT" allow-clear />
        </je-form-item>
        <je-form-item
          label="编码"
          name="DATASOURCE_CODE"
          :rules="[{ required: true }]"
          class="source-code"
        >
          <je-input v-model:value="sourceForm.DATASOURCE_CODE" disabled="disabled" allow-clear />
        </je-form-item>
      </je-form>
    </div>
    <div class="head-right">
      <div
        v-show="
          sourceForm.DATASOURCE_TYPE == sourceTypeObj.viewType ||
          sourceForm.DATASOURCE_TYPE == sourceTypeObj.sqlType
        "
        @click="handleImportSource"
      >
        <i class="fal fa-database"></i>
        <span>{{
          sourceForm.DATASOURCE_TYPE == sourceTypeObj.viewType ? '引入资源表' : '引入资源表'
        }}</span>
      </div>
      <div @click="handleExec">
        <i class="fas fa-play icon-play"></i>
        <span>执行</span>
      </div>
      <div @click="handleRecover">
        <i class="jeicon jeicon-return-restore-o"></i>
        <span>复原</span>
      </div>
      <div @click="handleSave">
        <i class="fal fa-save"></i>
        <span>保存</span>
      </div>
    </div>
  </div>
</template>

<script>
  import { defineComponent, reactive, toRefs, onMounted, watch } from 'vue';
  import { Form, Input, Modal } from '@jecloud/ui';
  import { SelectWindow } from '@jecloud/func';
  import { execScript, isPromise, isNotEmpty } from '@jecloud/utils';
  import { useJE } from '@common/helper/je';
  import VueEvent from '@/helper/event';
  import { showModal } from '@/views/data-source/common/execModal.jsx';
  import { useSourceStore } from '@/store/source-store';
  import { DataSource } from '@/views/data-source/model/data-source';
  import {
    SOURCE_TYPE_ICON,
    SOURCE_RELOAD_TREE,
    SOURCE_UPDATE_NAME,
    SOURCE_START_RECOVER,
  } from '@/helper/constant';
  import {
    updateSourceAndModule,
    sourceExecuteSql,
    getExecuteSqlFields,
    getSourceDataById,
  } from '@/api/index';
  const useForm = Form.useForm;
  export default defineComponent({
    name: 'SourceHead',
    components: {
      JeForm: Form,
      JeFormItem: Form.Item,
      JeInput: Input,
    },
    props: {
      saveAction: {
        type: Boolean,
        default: () => {
          return false;
        },
      },
      execAction: {
        type: Boolean,
        default: () => {
          return false;
        },
      },
    },
    emits: [
      'selectTableData',
      'selectTableSource',
      'validateAction',
      'valedateExecAction',
      'reloadViewRelevan',
    ],
    setup(props, { emit }) {
      const sourceStore = useSourceStore(); //数据源全局状态
      const curSourceCode = sourceStore.getSourceCode();
      const curSourceData = sourceStore.getSourceDataByCode(curSourceCode);
      const state = reactive({
        sourceTypeObj: SOURCE_TYPE_ICON, //数据源类型对象
        sourceForm: {
          DATASOURCE_TEXT: curSourceData.DATASOURCE_TEXT, //数据源名称
          DATASOURCE_CODE: curSourceData.DATASOURCE_CODE, //数据源code
          DATASOURCE_TYPE: curSourceData.DATASOURCE_TYPE, //数据源类型
        },
        rulesRef: {
          DATASOURCE_TEXT: [
            {
              required: true,
              message: '该输入项为必填项',
            },
          ],
        },
      });
      const { validate, validateInfos } = useForm(state.sourceForm, state.rulesRef, {
        onValidate: (...args) => {},
      });
      //执行js语句返回结果
      const execScriptResult = (executeJs) => {
        let flag = true;
        if (isNotEmpty(executeJs)) {
          flag = execScript(executeJs, {
            EventOptions: { isTrusted: true, type: 'dataSource', params: {} },
            JE: useJE(),
          });
          if (isPromise(flag)) {
            return flag;
          } else {
            return flag !== false ? Promise.resolve(flag) : Promise.reject();
          }
        } else {
          return Promise.resolve();
        }
      };
      const methods = {
        handleImportSource() {
          let sourceConfigStr = '';
          switch (state.sourceForm.DATASOURCE_TYPE) {
            case state.sourceTypeObj.viewType: //图形数据源类型多选
              sourceConfigStr = 'JE_CORE_RESOURCETABLE,,,M';
              break;
            case state.sourceTypeObj.sqlType: //SQL数据源类型
              sourceConfigStr = 'JE_CORE_RESOURCETABLE,,,S';
              break;
            default:
              break;
          }
          //引入资源表
          SelectWindow.show({
            title: '引入资源表',
            type: 'tree',
            width: 400,
            height: 600,
            selectExp: "'{nodeInfo}' != 'MODULE'",
            // showButton: false,// 隐藏操作按钮
            configInfo: sourceConfigStr,
            querys: [
              {
                code: 'SY_PRODUCT_ID',
                type: '=',
                value: curSourceData.SY_PRODUCT_ID,
                cn: 'and',
              },
            ],
            callback: function ({ rows, config, $modal }) {
              switch (state.sourceForm.DATASOURCE_TYPE) {
                case state.sourceTypeObj.viewType: //图形数据源类型多选
                  emit('selectTableData', rows);
                  break;
                case state.sourceTypeObj.sqlType: //SQL数据源类型
                  emit('selectTableSource', rows[0]);
                  break;
                default:
                  break;
              }
            },
          });
        },
        handleSave() {
          //保存操作
          validate()
            .then(() => {
              // const curSourceCode = sourceStore.getSourceCode();
              const curSourceData = sourceStore.getSourceDataByCode(curSourceCode);
              curSourceData.DATASOURCE_TEXT = state.sourceForm.DATASOURCE_TEXT;
              switch (curSourceData.DATASOURCE_TYPE) {
                case 'VIEW':
                  //输出字段
                  if (curSourceData.DATASOURCE_OUTPUT_COLUMNS) {
                    const outputColumns = JSON.parse(curSourceData.DATASOURCE_OUTPUT_COLUMNS);
                    if (outputColumns && outputColumns.length > 0) {
                      //结果集不为空
                      if (curSourceData.DATASOURCE_RESULT_CONFIG) {
                        const resultConfig = JSON.parse(curSourceData.DATASOURCE_RESULT_CONFIG);
                        resultConfig.fields = [];
                        outputColumns.forEach((item) => {
                          resultConfig.fields.push(item.name);
                        });
                        curSourceData.DATASOURCE_RESULT_CONFIG = JSON.stringify(resultConfig);
                      } else {
                        const resultObj = { fields: [] };
                        outputColumns.forEach((item) => {
                          resultObj.fields.push(item.name);
                        });
                        curSourceData.DATASOURCE_RESULT_CONFIG = JSON.stringify(resultObj);
                      }
                    } else {
                      Modal.alert('请先操作【引入资源表】并进行选择表数据信息！', 'warning');
                      return false;
                    }
                  } else {
                    Modal.alert('请先操作【引入资源表】并进行选择表数据信息！', 'warning');
                    return false;
                  }
                  //参数
                  if (curSourceData.DATASOURCE_CONFIG) {
                    const queryConfig = JSON.parse(curSourceData.DATASOURCE_CONFIG);
                    const orderByStr = queryConfig.orderBy.trim();
                    if (orderByStr) {
                      const reg1 = /^order by/gi;
                      if (!reg1.test(orderByStr)) {
                        Modal.alert('请输入正确的排序条件: 以order by 开头', 'warning');
                        return false;
                      }
                      const str = 'order by ';
                      if (orderByStr.replace(/(^\s*)|(\s*$)/g, '').length <= str.length) {
                        Modal.alert('请输入正确的排序条件', 'warning');
                        return false;
                      }
                    }
                    //自定义条件
                    const jsScript = queryConfig.jsScript;
                    if (jsScript) {
                      execScriptResult(jsScript).then((res) => {
                        if (res) {
                          const execObj = {};
                          //存在参数
                          if (res.params) {
                            execObj.params = res.params;
                          }
                          //存在执行条件
                          if (res.querys) {
                            execObj.querys = res.querys;
                          }
                          queryConfig.editorFn = execObj;
                        } else {
                          queryConfig.editorFn = '';
                        }
                        curSourceData.DATASOURCE_CONFIG = JSON.stringify(queryConfig);
                      });
                    }
                    setTimeout(() => {
                      methods.saveSourceData();
                    }, 0);
                  } else {
                    methods.saveSourceData();
                  }
                  break;
                case 'SQL':
                  methods.saveSourceData();
                  break;
                case 'JS':
                  execScriptResult(curSourceData.DATASOURCE_CONFIG).then((res) => {
                    if (res) {
                      if (res.fields && res.data) {
                        const exetObj = {
                          fields: res.fields.join(','),
                          params: res.params || [],
                        };
                        curSourceData.DATASOURCE_RESULT_CONFIG = JSON.stringify(exetObj);
                        methods.saveSourceData();
                      } else {
                        Modal.alert('执行结果不符合返回数据格式', 'warning');
                      }
                    } else {
                      Modal.alert('执行结果出错', 'warning');
                    }
                  });
                  break;
                case 'ACTION':
                case 'SERVICE': //Service数据源
                case 'TABLE': //资源表数据源
                  //action或service必填项是否输入
                  emit('validateAction');
                  break;
                default:
                  break;
              }
            })
            .catch((err) => {
              console.log('error', err);
            });
        },
        //保存数据
        saveSourceData() {
          // const curSourceCode = sourceStore.getSourceCode();
          const curSourceData = sourceStore.getSourceDataByCode(curSourceCode);
          if (curSourceData.DATASOURCE_TYPE == 'VIEW') {
            if (curSourceData.DATASOURCE_BASE_TABLE) {
              const tableList = JSON.parse(curSourceData.DATASOURCE_BASE_TABLE);
              curSourceData.masterTableId = tableList.length > 0 ? tableList[0].tableId : 0;
            }
          }
          updateSourceAndModule(curSourceData).then((res) => {
            if (res.success) {
              //关闭弹窗
              VueEvent.emit(SOURCE_RELOAD_TREE);
              //变更保存的资源名称
              VueEvent.emit(SOURCE_UPDATE_NAME, curSourceData);
              //当视图模式时，保存成功需要重新load关联设计
              emit('reloadViewRelevan');
              Modal.notice('保存成功', 'success');
              //更新缓存的复原数据
              // sourceStore.setRecoverSourceDataByCode(curSourceCode, curSourceData);
            } else {
              Modal.alert(res.message, 'error');
            }
          });
        },
        //保存并执行
        saveSourceAndExec() {
          const curSourceCode = sourceStore.getSourceCode();
          const curSourceData = sourceStore.getSourceDataByCode(curSourceCode);
          if (curSourceData.DATASOURCE_TYPE == 'VIEW') {
            if (curSourceData.DATASOURCE_BASE_TABLE) {
              const tableList = JSON.parse(curSourceData.DATASOURCE_BASE_TABLE);
              curSourceData.masterTableId = tableList.length > 0 ? tableList[0].tableId : 0;
            }
          }
          updateSourceAndModule(curSourceData).then((res) => {
            if (res.success) {
              //关闭弹窗
              VueEvent.emit(SOURCE_RELOAD_TREE);
              //更新缓存的复原数据
              // sourceStore.setRecoverSourceDataByCode(curSourceCode, curSourceData);
              //执行结果
              const exetObj = {
                fields: [],
                data: [],
                params: [],
              };
              //获取执行sql字段
              getExecuteSqlFields({
                JE_CORE_DATASOURCE_ID: curSourceData.JE_CORE_DATASOURCE_ID,
              }).then((res) => {
                if (res.success) {
                  exetObj.fields = res.data ? res.data.split(',') : [];
                  const viewParam = {
                    config: curSourceData.JE_CORE_DATASOURCE_ID,
                    parameterStr: '',
                    limit: 30,
                    JE_CORE_DATASOURCE_ID: curSourceData.JE_CORE_DATASOURCE_ID,
                  };
                  sourceExecuteSql(viewParam)
                    .then((res) => {
                      if (res.success) {
                        exetObj.data = res.data.listData;
                        showModal({ execData: exetObj });
                      } else {
                        // showModal({ execData: exetObj });
                        Modal.alert(res.message, 'error');
                      }
                    })
                    .catch((err) => {
                      showModal({ execData: exetObj });
                    });
                } else {
                  Modal.alert(res.message, 'error');
                }
              });
            } else {
              Modal.alert(res.message, 'error');
            }
          });
        },
        //显示执行sql弹窗选择
        showExecSqlModal() {
          Modal.dialog({
            title: '消息',
            status: 'warning',
            content: '是否要进行执行操作？',
            buttons: [
              {
                text: '执行并保存',
                type: 'primary',
                closable: false,
                handler: ({ button, $modal }) => {
                  button.loading = true;
                  setTimeout(() => {
                    button.loading = false;
                    methods.saveSourceAndExec();
                    $modal.close();
                  }, 1000);
                },
              },
              {
                text: '仅保存',
                type: 'primary',
                closable: false,
                handler: ({ button, $modal }) => {
                  button.disabled = true;
                  setTimeout(() => {
                    button.disabled = false;
                    methods.saveSourceData();
                    $modal.close();
                  }, 1000);
                },
              },
              {
                text: '取消',
              },
            ],
          });
        },
        //执行操作
        handleExec() {
          const curSourceCode = sourceStore.getSourceCode();
          const curSourceData = sourceStore.getSourceDataByCode(curSourceCode);
          switch (curSourceData.DATASOURCE_TYPE) {
            case 'VIEW': //视图
              //判断是否有输出视图
              if (curSourceData.DATASOURCE_OUTPUT_COLUMNS != '') {
                const outputColumns = JSON.parse(curSourceData.DATASOURCE_OUTPUT_COLUMNS);
                if (outputColumns.length > 0) {
                  const strParamData = {
                    masterTableId: '',
                    viewColumn: [],
                    viewOuput: [],
                    queryConfigVo: [],
                  };
                  //关系表
                  if (curSourceData.DATASOURCE_BASE_TABLE) {
                    const tableList = JSON.parse(curSourceData.DATASOURCE_BASE_TABLE);
                    strParamData.masterTableId = tableList[0]?.tableId;
                  }
                  //关联设计
                  if (curSourceData.DATASOURCE_INCIDENCE_RELATION) {
                    const viewColumn = JSON.parse(curSourceData.DATASOURCE_INCIDENCE_RELATION);
                    strParamData.viewColumn = viewColumn;
                  }
                  //输出视图
                  if (curSourceData.DATASOURCE_OUTPUT_COLUMNS) {
                    const viewOuput = JSON.parse(curSourceData.DATASOURCE_OUTPUT_COLUMNS);
                    strParamData.viewOuput = viewOuput;
                  }
                  //查询配置
                  if (curSourceData.DATASOURCE_CONFIG) {
                    const queryConfigVo = JSON.parse(curSourceData.DATASOURCE_CONFIG);
                    //排序条件
                    const orderByStr = queryConfigVo.orderBy.trim();
                    if (orderByStr) {
                      const reg1 = /^order by/gi;
                      if (!reg1.test(orderByStr)) {
                        Modal.alert('请输入正确的排序条件: 以order by 开头', 'warning');
                        return false;
                      }
                      const str = 'order by ';
                      if (orderByStr.replace(/(^\s*)|(\s*$)/g, '').length <= str.length) {
                        Modal.alert('请输入正确的排序条件', 'warning');
                        return false;
                      }
                    }
                    if (queryConfigVo.jsScript) {
                      execScriptResult(queryConfigVo.jsScript).then((res) => {
                        if (res) {
                          const execObj = {};
                          //存在参数
                          if (res.params) {
                            execObj.params = res.params;
                          }
                          //存在执行条件
                          if (res.querys) {
                            execObj.querys = res.querys;
                          }
                          queryConfigVo.editorFn = execObj;
                        } else {
                          queryConfigVo.editorFn = '';
                        }
                        strParamData.queryConfigVo = queryConfigVo;
                      });
                    } else {
                      strParamData.queryConfigVo = queryConfigVo;
                    }
                    setTimeout(() => {
                      const viewParam = {
                        config: JSON.stringify(strParamData),
                        parameterStr: '',
                        limit: 30,
                        JE_CORE_DATASOURCE_ID: curSourceData.JE_CORE_DATASOURCE_ID,
                      };
                      sourceExecuteSql(viewParam).then((res) => {
                        const exetObj = {
                          fields: [],
                          data: [],
                          params: [],
                        };
                        if (curSourceData.DATASOURCE_OUTPUT_COLUMNS) {
                          const outputColumns = JSON.parse(curSourceData.DATASOURCE_OUTPUT_COLUMNS);
                          outputColumns.forEach((item) => {
                            exetObj.fields.push(item.name);
                          });
                        }
                        if (res.success) {
                          exetObj.data = res.data.listData;
                          showModal({ execData: exetObj });
                        } else {
                          // showModal({ execData: exetObj });
                          Modal.alert(res.message, 'error');
                        }
                      });
                    }, 0);
                  } else {
                    const viewParam = {
                      config: JSON.stringify(strParamData),
                      parameterStr: '',
                      limit: 30,
                      JE_CORE_DATASOURCE_ID: curSourceData.JE_CORE_DATASOURCE_ID,
                    };
                    sourceExecuteSql(viewParam).then((res) => {
                      const exetObj = {
                        fields: [],
                        data: [],
                        params: [],
                      };
                      if (curSourceData.DATASOURCE_OUTPUT_COLUMNS) {
                        const outputColumns = JSON.parse(curSourceData.DATASOURCE_OUTPUT_COLUMNS);
                        outputColumns.forEach((item) => {
                          exetObj.fields.push(item.name);
                        });
                      }
                      if (res.success) {
                        exetObj.data = res.data.listData;
                        showModal({ execData: exetObj });
                      } else {
                        // showModal({ execData: exetObj });
                        Modal.alert(res.message, 'error');
                      }
                    });
                  }
                } else {
                  Modal.alert('请先操作【引入资源表】并进行选择表数据信息！', 'warning');
                }
              } else {
                Modal.alert('请先操作【引入资源表】并进行选择表数据信息！', 'warning');
              }
              break;
            case 'SQL': //SQL
              if (curSourceData.DATASOURCE_DATA) {
                //已保存过的数据
                const curRecoverSourceData = sourceStore.getRecoverSourceDataByCode(curSourceCode);
                //执行sql判断当前sql是否和保存过的一致
                if (curRecoverSourceData && curRecoverSourceData.DATASOURCE_DATA) {
                  if (curRecoverSourceData.DATASOURCE_DATA == curSourceData.DATASOURCE_DATA) {
                    //获取执行sql字段
                    getExecuteSqlFields({
                      JE_CORE_DATASOURCE_ID: curSourceData.JE_CORE_DATASOURCE_ID,
                    }).then((res) => {
                      const exetObj = {};
                      if (res.success) {
                        exetObj.fields = res.data ? res.data.split(',') : [];
                        const viewParam = {
                          config: curSourceData.JE_CORE_DATASOURCE_ID,
                          parameterStr: '',
                          limit: 30,
                          JE_CORE_DATASOURCE_ID: curSourceData.JE_CORE_DATASOURCE_ID,
                        };
                        sourceExecuteSql(viewParam)
                          .then((res) => {
                            if (res.success) {
                              exetObj.data = res.data.listData;
                              showModal({ execData: exetObj });
                            } else {
                              // showModal({ execData: exetObj });
                              Modal.alert(res.message, 'error');
                            }
                          })
                          .catch((err) => {
                            // showModal({ execData: exetObj });
                            Modal.alert(res.message, 'error');
                          });
                      } else {
                        Modal.alert(res.message, 'error');
                      }
                    });
                  } else {
                    methods.showExecSqlModal();
                  }
                } else {
                  methods.showExecSqlModal();
                }
              } else {
                Modal.alert('请先操作【引入资源表】信息！', 'warning');
              }
              break;
            case 'JS':
              if (curSourceData.DATASOURCE_CONFIG) {
                execScriptResult(curSourceData.DATASOURCE_CONFIG).then((res) => {
                  if (res?.fields && res?.data) {
                    setTimeout(() => {
                      const exetObj = {
                        fields: res.fields,
                        data: res.data,
                        params: res.params || [],
                      };
                      showModal({ execData: exetObj });
                    });
                  } else {
                    Modal.alert('执行结果不符合返回数据格式', 'warning');
                  }
                });
              } else {
                Modal.alert('请先进行JS编写', 'warning');
              }
              break;
            case 'ACTION': //接口ACTION
            case 'SERVICE': //service数据源
              emit('valedateExecAction');
              break;
            case 'TABLE': //资源表数据源
              //数据配置
              const strParamData = {
                queryConfigVo: [],
              };
              if (curSourceData.DATASOURCE_CONFIG) {
                const queryConfigVo = JSON.parse(curSourceData.DATASOURCE_CONFIG);
                if (queryConfigVo.tableName) {
                  //排序条件
                  const orderByStr = queryConfigVo.orderBy.trim();
                  if (orderByStr) {
                    const reg1 = /^order by/gi;
                    if (!reg1.test(orderByStr)) {
                      Modal.alert('请输入正确的排序条件: 以order by 开头', 'warning');
                      return false;
                    }
                    const str = 'order by ';
                    if (orderByStr.replace(/(^\s*)|(\s*$)/g, '').length <= str.length) {
                      Modal.alert('请输入正确的排序条件', 'warning');
                      return false;
                    }
                  }
                  if (queryConfigVo.jsScript) {
                    execScriptResult(queryConfigVo.jsScript).then((res) => {
                      if (res) {
                        const execObj = {};
                        //存在参数
                        if (res.params) {
                          execObj.params = res.params;
                        }
                        //存在执行条件
                        if (res.querys) {
                          execObj.querys = res.querys;
                        }
                        queryConfigVo.editorFn = execObj;
                      } else {
                        queryConfigVo.editorFn = '';
                      }
                      strParamData.queryConfigVo = queryConfigVo;
                    });
                  } else {
                    strParamData.queryConfigVo = queryConfigVo;
                  }
                  setTimeout(() => {
                    const viewParam = {
                      config: JSON.stringify(strParamData),
                      parameterStr: '',
                      limit: 30,
                      JE_CORE_DATASOURCE_ID: curSourceData.JE_CORE_DATASOURCE_ID,
                    };
                    sourceExecuteSql(viewParam).then((res) => {
                      const exetObj = {
                        fields: [],
                        data: [],
                        params: [],
                      };
                      if (curSourceData.DATASOURCE_OUTPUT_COLUMNS) {
                        const outputColumns = JSON.parse(curSourceData.DATASOURCE_OUTPUT_COLUMNS);
                        outputColumns.forEach((item) => {
                          exetObj.fields.push(item.name);
                        });
                      }
                      if (res.success) {
                        exetObj.data = res.data.listData;
                        showModal({ execData: exetObj });
                      } else {
                        // showModal({ execData: exetObj });
                        Modal.alert(res.message, 'error');
                      }
                    });
                  }, 0);
                } else {
                  Modal.alert('请先选择资源表', 'warning');
                }
              } else {
                const viewParam = {
                  config: JSON.stringify(strParamData),
                  parameterStr: '',
                  limit: 30,
                  JE_CORE_DATASOURCE_ID: curSourceData.JE_CORE_DATASOURCE_ID,
                };
                sourceExecuteSql(viewParam).then((res) => {
                  const exetObj = {
                    fields: [],
                    data: [],
                    params: [],
                  };
                  if (curSourceData.DATASOURCE_OUTPUT_COLUMNS) {
                    const outputColumns = JSON.parse(curSourceData.DATASOURCE_OUTPUT_COLUMNS);
                    outputColumns.forEach((item) => {
                      exetObj.fields.push(item.name);
                    });
                  }
                  if (res.success) {
                    exetObj.data = res.data.listData;
                    showModal({ execData: exetObj });
                  } else {
                    // showModal({ execData: exetObj });
                    Modal.alert(res.message, 'error');
                  }
                });
              }
              break;
            default:
              break;
          }
        },
        //开始action方式执行
        execActionMethod() {
          const curSourceCode = sourceStore.getSourceCode();
          const curSourceData = sourceStore.getSourceDataByCode(curSourceCode);
          //执行结果
          const exetObj = {
            fields: [],
            data: [],
            params: [],
          };
          const configData = JSON.parse(curSourceData.DATASOURCE_CONFIG);
          exetObj.fields = configData.fieldName.split(',');
          const viewParam = {
            config: curSourceData.DATASOURCE_CONFIG,
            parameterStr: '',
            limit: 30,
            JE_CORE_DATASOURCE_ID: curSourceData.JE_CORE_DATASOURCE_ID,
          };
          sourceExecuteSql(viewParam)
            .then((res) => {
              if (res.success) {
                exetObj.data = res.data.listData;
                showModal({ execData: exetObj });
              } else {
                // showModal({ execData: exetObj });
                Modal.alert(res.message, 'warning');
              }
            })
            .catch((err) => {
              showModal({ execData: exetObj });
            });
        },
        //复原操作
        handleRecover() {
          const curSourceCode = sourceStore.getSourceCode();
          const curSourceData = sourceStore.getSourceDataByCode(curSourceCode);
          Modal.confirm({
            title: '复原',
            content: `是否确认将数据源“${curSourceData.DATASOURCE_TEXT}”所有操作配置项信息复原到操作前（上一次保存后进行的操作）的状态？`,
            okButton: () => {
              const sourceParam = {
                tableCode: 'JE_CORE_DATASOURCE',
                pkValue: curSourceData.JE_CORE_DATASOURCE_ID,
              };
              //获取当前数据源数据
              getSourceDataById(sourceParam).then((res) => {
                if (res.success) {
                  const curSource = new DataSource(res.data);
                  sourceStore.setSourceDataByCode(res.data.DATASOURCE_CODE, curSource);
                  state.sourceForm.DATASOURCE_TEXT = res.data.DATASOURCE_TEXT; //数据源名称
                  state.sourceForm.DATASOURCE_CODE = res.data.DATASOURCE_CODE; //数据源code
                  state.sourceForm.DATASOURCE_TYPE = res.data.DATASOURCE_TYPE; //数据源类型
                  VueEvent.emit(SOURCE_START_RECOVER, curSourceCode);
                  Modal.notice({ content: '复原成功', status: 'success' });
                } else {
                  Modal.alert(res.message, 'error');
                }
              });
            },
          });
        },
      };
      watch(
        () => props.saveAction,
        (val) => {
          if (val) {
            methods.saveSourceData();
          }
        },
      );
      watch(
        () => props.execAction,
        (val) => {
          if (val) {
            methods.execActionMethod();
          }
        },
      );

      onMounted(() => {});
      return {
        validate,
        validateInfos,
        ...toRefs(state),
        ...methods,
      };
    },
  });
</script>
<style scoped lang="less">
  .module-head-box {
    width: 100%;
    height: 40px;
    min-width: 900px;
    overflow: auto;
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-sizing: border-box;
    border-top: 1px solid #ecedf0;
    border-bottom: 1px solid #ecedf0;
    padding: 0 10px;
    .head-left {
      .ant-form {
        display: flex;
        align-items: center;
        .source-code {
          margin-left: 20px;
        }
      }
    }
    .head-right {
      display: flex;
      width: calc(100% - 250px);
      justify-content: flex-end;
      div {
        height: 32px;
        box-sizing: border-box;
        border: 1px solid #ecedf0;
        border-radius: 4px;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-left: 10px;
        padding: 0 12px;
        cursor: pointer;
        .icon-play {
          color: #fac502;
        }
        span {
          padding-left: 7px;
        }
      }
      div:hover {
        background: rgba(0, 0, 0, 0.08);
      }
    }
  }
</style>
